---
title: "Dicument polarity"
author: "Kohei Watanabe"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---

```{r, message=FALSE}
knitr::opts_chunk$set(echo = TRUE, dpi = 150, fig.height = 6,
                      fig.width = 10, collapse = TRUE)
source("functions.R")
require(quanteda)
require(LSX)
require(locfit)
lss <- readRDS("lss.RDS")
dat <- readRDS('class.RDS')
dat <- subset(dat, dat$country.class != "us")
```

```{r fig.height=5, fig.width=10}
par(mar = c(2, 4, 1, 1))
set.seed(1234)
dat_sub <- dat[sample(seq(nrow(dat)), 10000),]
smooth <- predict(locfit(lss ~ lp(year, nn = 0.05), data = dat),
                  newdata = data.frame(year = seq(1861, 2017)))
plot(dat_sub$year, dat_sub$lss, 
     pch = 16, col = rgb(0, 0, 0, 0.1), 
     ylim = c(-2, 2), xlab = "", ylab = "Polarity score", xaxt = "n")
axis(1, seq(1850, 2020, 10))
lines(seq(1861, 2017), smooth)
abline(h = 0)
```

```{r fig.height=6, fig.width=10}
par(mfrow = c(2, 1), mar = c(2, 4, 2, 2), font.main = 1)
par(mar = c(0.5, 4, 2, 2))

dat_de <- subset(dat_sub, country.class == "de") 
plot(dat_sub$year, dat_sub$lss, 
     pch = 16,
     col = rgb(0, 0, 0, 0.05), 
     ylim = c(-2, 2), xlab = "", ylab = "Polarity score", xaxt = "n")
points(dat_de$year, dat_de$lss, pch = 16, col = rgb(0, 0, 0, 0.5))
abline(h = 0)
legend("topleft", pch = 16, col = rgb(0, 0, 0, c(0.5, 0.05)), 
       legend = c("Germany", "Other countries"))

par(mar = c(2, 4, 0.5, 2))
gti <- get_gti(dat_sub, "de")
plot(as.numeric(names(gti)), gti, type = "b", xaxt = "n",
     ylab = "Threat index")
axis(1, seq(1850, 2020, 10))
```
